package com.sun.controller;

import com.sun.enums.HttpEnum;
import com.sun.mapper.PurchaseOrdersMapper;
import com.sun.pojo.dto.PurchaseOrderDetailsDTO;
import com.sun.pojo.dto.PurchaseOrderDetailsDeliverDTO;
import com.sun.pojo.entity.PurchaseOrderDetails;
import com.sun.pojo.vo.R;
import com.sun.service.PurchaseOrderDetailsService;
import com.sun.service.PurchaseOrdersService;
import com.sun.service.PurchaseSettlementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 采购明细表 记录每个采购订单的具体商品信息。(PurchaseOrderDetails)表控制层
 *
 * @author makejava
 * @since 2024-10-25 08:18:09
 */
@RestController
@RequestMapping("purchaseOrderDetails")
@CrossOrigin
public class PurchaseOrderDetailsController {
    /**
     * 服务对象
     */
    @Autowired
    PurchaseOrderDetailsService purchaseOrderDetailsService;
    @Autowired
    PurchaseOrdersService purchaseOrdersService;
    @Autowired
    PurchaseSettlementService purchaseSettlementService;

    /**
     * 分页查询
     *
     * @param purchaseOrderDetails 筛选条件
     * @param pageNum      分页对象
     * @return 查询结果
     */
    @GetMapping
    public R queryByPage(PurchaseOrderDetails purchaseOrderDetails, Integer pageNum) {
        return R.success(purchaseOrderDetailsService.queryByPage(purchaseOrderDetails, pageNum));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R queryById(@PathVariable("id") String id) {
        return R.success(purchaseOrderDetailsService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param purchaseOrderDetail 实体
     * @return 新增结果
     */
    @PostMapping
    public R add(@RequestBody PurchaseOrderDetailsDTO purchaseOrderDetail) {
        List<PurchaseOrderDetailsDTO>  purchaseOrderDetails = purchaseOrderDetail.getPurchaseOrderDetailsDTOList();
        return R.success(purchaseOrderDetailsService.insert(purchaseOrderDetails));
    }

    /**
     * 编辑数据
     *
     * @param purchaseOrderDetails 实体
     * @return 编辑结果
     */
    @PutMapping
    public R edit(@RequestBody PurchaseOrderDetails purchaseOrderDetails) {
        return R.success(purchaseOrderDetailsService.update(purchaseOrderDetails));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("{id}")
    public R deleteById(@PathVariable("id") String id) {
        if (purchaseOrdersService.deleteById(id)>0) {
            if (purchaseSettlementService.deleteById(Integer.valueOf(id))>0) {
                return R.success(purchaseOrderDetailsService.deleteById(id));
            }
        }
        return R.fail(HttpEnum.WARN.getCode(),"删除失败，请先删除该采购订单下的商品信息");
    }

    /**
     * 通过主键查询订单
     *
     * @param orderId 主键
     * @return 是否成功
     */
    @GetMapping("getAll/{id}")
    public R selectDetailAll(@PathVariable("id") String orderId){
        return purchaseOrderDetailsService.selectDetailsAll(orderId);
    }

    @GetMapping("getBySupplierId")
    public R getBySupplierId(String supplierId,Integer status){
        return purchaseOrderDetailsService.selectBySupplierId(supplierId,status);
    }

    @PostMapping("/deliver")
    public R deliver(@RequestBody PurchaseOrderDetailsDeliverDTO dto){
        return purchaseOrderDetailsService.deliver(dto);
    }

    /*
    获取库存
     */
    @GetMapping("/getStock")
    public R getStock(String productName){
        return purchaseOrderDetailsService.selectStatus2(productName);
    }

    @GetMapping("selectSuppliersMoneys")
    public R selectSuppliersMoneys(String supplierId,String updatedBy){
        return purchaseOrderDetailsService.selectSuppliersMoneys(supplierId,updatedBy);
    }
}

